package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {
    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    @Override
    public List<JSONObject> getBaseCategoryList() {

        //查寻所有的分类视图数据
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
        //将所有的视图数据根据catgroyId进行分类
        //2.处理一级分类列表
        //2.1 按照1级分类ID进行分组 得到Map集合 Key:1级分类ID  Val：对应1级分类列表
        Map<Long, List<BaseCategoryView>> category1Map =
                baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        //2.2定义一级分类集合对象
        List<JSONObject> list1 = new ArrayList<>();
        //2.3 遍历所有的一级分类对象，取出其key 和 value
        for (Map.Entry<Long, List<BaseCategoryView>> entry1 : category1Map.entrySet()) {
            //2.2.1 创建一个一级分类对象
            JSONObject Category1 = new JSONObject();
            //2.2.2 获取一级分类的key
            Long category1Id = entry1.getKey();
            //2.2.3 获取一级分类的value
            String category1Name = entry1.getValue().get(0).getCategory1Name();
            //添加key，value到一级分类对象中
            Category1.put("categoryId", category1Id);
            Category1.put("categoryName", category1Name);
            //2.3 处理二级分类
            //创建二级分类对象集合
            List<JSONObject> list2 = new ArrayList<>();
            //2.3.1 按照2级分类ID进行分组 得到Map集合 Key:2级分类ID  Val：对应2级分类列表
            Map<Long, List<BaseCategoryView>> category2Map =
                    entry1.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
                //创建二级分类对象
                JSONObject Category2 = new JSONObject();
                //2.3.2 获取二级分类的key
                Long category2Id = entry2.getKey();
                //2.3.3 获取二级分类的key
                String category2Name = entry2.getValue().get(0).getCategory2Name();
                //添加key，value到二级分类对象中
                Category2.put("categoryId", category2Id);
                Category2.put("categoryName", category2Name);
                //2.4 所有的二级分类的集合都是三级分类的数据，直接遍历二级分类获得三级分类的key 和 value
                List<JSONObject> list3 = new ArrayList<>();
                for (BaseCategoryView baseCategoryView : entry2.getValue()) {
                    //2.4.1 构建三级分类JSON对象
                    JSONObject Category3 = new JSONObject();
                    //2.4.2 获取三级分类ID
                    Long category3Id = baseCategoryView.getCategory3Id();
                    //2.4.3 获取三级分类名称
                    String category3Name = baseCategoryView.getCategory3Name();
                    Category3.put("categoryId", category3Id);
                    Category3.put("categoryName", category3Name);
                    //4.1.4 将三级分类JSON对象装入集合中
                    list3.add(Category3);
                }
                list2.add(Category2);
                //把三级分类对象添加到二级分类对象的 categoryChild 属性中
                Category2.put("categoryChild", list3);
            }
            //把二级分类对象添加到一级分类对象的 categoryChild 属性中
            Category1.put("categoryChild", list2);
            //把一级分类对象添加到一级分类对象集合中
            list1.add(Category1);
        }
        return list1;
    }

    @Override
    public List<BaseAttribute> getAttributeByCategory1Id(Long category1Id) {
        List<BaseAttribute> baseAttributeList = baseAttributeMapper.getAttributeByCategory1Id(category1Id);
        return baseAttributeList;
    }

    @Override
    public BaseCategoryView getCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    /***
     * 根据1级分类ID查询该分类下前7个置顶3级分类列表
     * @param category1Id
     * @return
     */
    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        //根据一级分类id查询一级分类列表
        LambdaQueryWrapper<BaseCategory2> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(BaseCategory2::getCategory1Id, category1Id);
        List<BaseCategory2> baseCategory2 = baseCategory2Mapper.selectList(lambdaQueryWrapper);
        //便利获取二级分类id
        if (CollectionUtil.isNotEmpty(baseCategory2)) {
			List<Long> baseCategory2IdList = baseCategory2.stream().map(BaseCategory2::getId).collect(Collectors.toList());
			LambdaQueryWrapper<BaseCategory3> lambdaQueryWrapperBy3Id = new LambdaQueryWrapper();
            //获取三级分类的列表信息
            lambdaQueryWrapperBy3Id.in(BaseCategory3::getCategory2Id, baseCategory2IdList);
			lambdaQueryWrapperBy3Id.eq(BaseCategory3::getIsTop,"1");
			lambdaQueryWrapperBy3Id.orderByAsc(BaseCategory3::getOrderNum);
			lambdaQueryWrapperBy3Id.last("limit 7");
            List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(lambdaQueryWrapperBy3Id);
            return baseCategory3List;
        }
		return null;
    }

    /**
     * 根据1级分类ID查询包含二级分类以及三级分类
     *
     * @param category1Id
     * @return
     */
    @Override
    public JSONObject getBaseCategoryListByCategory1Id(Long category1Id) {
        //1.封装一级分类对象
        LambdaQueryWrapper<BaseCategoryView> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryView::getCategory1Id, category1Id);
        List<BaseCategoryView> category1List = baseCategoryViewMapper.selectList(queryWrapper);
        BaseCategoryView baseCategoryView = category1List.get(0);
        //1.1 封装一级分类对象
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("categoryId", baseCategoryView.getCategory1Id());
        jsonObject1.put("categoryName", baseCategoryView.getCategory1Name());
        //2.处理一级分类下包含二级分类列表
        //2.1 对“一级分类集合”中二级分类ID进行分组
        Map<Long, List<BaseCategoryView>> category2Map =
                category1List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
        if (CollectionUtil.isNotEmpty(category2Map)) {
            List<JSONObject> jsonObjects2List = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
                //2.2 封装二级分类JSON对象
                Long category2Id = entry2.getKey();
                String category2Name = entry2.getValue().get(0).getCategory2Name();
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("categoryId", category2Id);
                jsonObject2.put("categoryName", category2Name);
                jsonObjects2List.add(jsonObject2);
                //3.处理二级分类下包含三级分类列表
                List<JSONObject> jsonObjects3List = new ArrayList<>();
                for (BaseCategoryView categoryView : entry2.getValue()) {
                    //3.1 构建三级分类JSON对象
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.put("categoryId", categoryView.getCategory3Id());
                    jsonObject3.put("categoryName", categoryView.getCategory3Name());
                    jsonObjects3List.add(jsonObject3);
                }
                //3.2 将三级分类JSON对象集合将入二级分类对象"categoryChild"属性中
                jsonObject2.put("categoryChild", jsonObjects3List);
            }
            //2.3 将二级分类JSON对象集合加入到一级分类对象"categoryChild"属性中
            jsonObject1.put("categoryChild", jsonObjects2List);
        }
        return jsonObject1;
    }
}
